package com.xiaoqi.store.service.impl;


import com.xiaoqi.store.entity.BaseTestExcel;
import com.xiaoqi.store.service.DownLoadExcelService;
import com.xiaoqi.store.util.BaseTestUtil;
import com.xiaoqi.store.util.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.stereotype.Service;
import org.springframework.util.unit.DataUnit;

import java.io.FileOutputStream;
import java.io.OutputStream;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

// 下载报表
@Service
@Slf4j
public class DownLoadExcelServiceImp implements DownLoadExcelService {

    @Override
    public void assembleExcel(){
        String[] header = {"ExecutionResult", "TestTime", "Tester"};
        BaseTestExcel baseTestExcel1 = new BaseTestExcel("Exception 111", DateUtil.formatDate(new Date()), BaseTestUtil.TESTNAME);
        BaseTestExcel baseTestExcel2 = new BaseTestExcel("Exception 222", DateUtil.formatDate(new Date()),BaseTestUtil.TESTNAME);
        BaseTestExcel baseTestExcel3 = new BaseTestExcel("Exception 333",DateUtil.formatDate(new Date()),BaseTestUtil.TESTNAME);

        List<BaseTestExcel> baseTestExcels = new ArrayList<>();
        baseTestExcels.add(baseTestExcel1);
        baseTestExcels.add(baseTestExcel2);
        baseTestExcels.add(baseTestExcel3);
        export(baseTestExcels,header);
    }

    private static void export(List<BaseTestExcel> parseMessage,String[] header){
        String[] fieldNames = fieldName(BaseTestExcel.class);
        Workbook wb = new HSSFWorkbook();
        int rowSize = 0;
        Sheet sheet = wb.createSheet();
        Row row = sheet.createRow(rowSize);
        for (int i = 0; i < header.length; i++) {
            row.createCell(i).setCellValue(header[i]);
        }
        try {
            for (int x = 0; x < parseMessage.size(); x++) {
                rowSize = 1;
                Row rowNew = sheet.createRow(rowSize + x);
                for (int i = 0; i < header.length; i++) {
                    BaseTestExcel user = parseMessage.get(x);
                    for (int i1 = 0; i1 < fieldNames.length; i1++) {
                        String methodName = "get" + fieldNames[i].substring(0,1).toUpperCase() + fieldNames[i].substring(1);//获取属性的get方法名
                        Method method = user.getClass().getMethod(methodName);
                        Object invoke = method.invoke(user);//获取属性值
                        rowNew.createCell(i).setCellValue(invoke.toString());
                        log.info("methodName===  "+methodName);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        OutputStream outputStream = null;
        try {
            outputStream = new FileOutputStream("aaa.xls");
            wb.write(outputStream);
        } catch (Exception e) {

        } finally {
            try {
                if (outputStream != null) {
                    outputStream.flush();
                    outputStream.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            try{
                if(wb != null){
                    wb.close();
                }
            } catch (Exception e){

            }
        }
    }

    private static String[] fieldName(Class clazz) {
        Field[] declaredFields = clazz.getDeclaredFields();
        String[] fieldNames = new String[declaredFields.length];
        for (int i = 0; i < declaredFields.length; i++) {
            fieldNames[i] = declaredFields[i].getName(); //通过反射获取属性名
        }
        return fieldNames;
    }


}
